package services

import (
	"errors"
	"hft/Database/admin"
	"hft/global"
)

type Area struct{}

func (*Area) CreateArea(data *admin.Area) (err error) {

	err = global.OC_DB.Create(&data).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*Area) EditArea(data *admin.Area) (err error) {

	err = global.OC_DB.Model(&admin.Area{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"id":        data.ID,
		"sort":      data.Sort,
		"area_type": data.AreaType,
		"max":       data.Max,
		"min":       data.Min,
	}).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*Area) GetAreaList() (list []*admin.Area, err error) {

	var searchList []*admin.Area

	global.OC_DB.Find(&searchList)

	return searchList, nil
}

func (*Area) Delete(ids []int) (err error) {

	var list []*admin.Area

	global.OC_DB.Find(&list, ids)

	global.OC_DB.Model(&admin.Area{}).Unscoped().Delete(&list)

	return nil

}

var AreaService = new(Area)
